const {ccclass, property} = cc._decorator;

@ccclass
export default class Video extends cc.Component {
    @property(cc.VideoPlayer)
    videoPlayer: cc.VideoPlayer = null;

    @property(cc.Sprite)
    sprite: cc.Sprite = null;

    private videoSpriteFrame: cc.SpriteFrame = new cc.SpriteFrame();

    start() {}

    update(dt) {
        if (this.videoPlayer) {
            this.videoPlayer.enableGrabTexture();
            // @ts-ignore
            this.videoPlayer.setShowRaw(false);
            // @ts-ignore
            let w = this.videoPlayer._impl.getFrameWidth();
            // @ts-ignore
            let h = this.videoPlayer._impl.getFrameHeight();
            // @ts-ignore
            if (this.videoSpriteFrame) {
                this.videoSpriteFrame.setTexture(this.videoPlayer._ccVideoTex, cc.rect(0, 0, w, h), false, cc.v2(0, 0), cc.size(w, h));
            }
        }

        if (this.sprite && this.videoSpriteFrame) {
            this.sprite.spriteFrame = this.videoSpriteFrame;
        }
    }

    onBtnPlay() {
        if (this.videoPlayer) {
            this.videoPlayer.play();
        }
    }
}
